package com.nuanshui.heatedloan.task.failover.strategy;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.task.dal.mapper.TaskJobFailMapper;
import com.nuanshui.heatedloan.task.facade.QuartzTaskManager;
import com.nuanshui.heatedloan.task.facade.bean.JobProvider;
import com.nuanshui.heatedloan.task.facade.model.TaskJobFail;
import com.nuanshui.heatedloan.task.failover.TaskFailMessage;
import com.nuanshui.heatedloan.task.failover.TaskfailoverStrategyType;

/**
 * 定时任务失败 重试策略
 * @param 
 * @author guohao
 * @date 2017年1月4日 下午4:08:10
 * @since $
 */
public class TaskFailRetryStrategy extends AbstractTaskFailStrategy {

	private Logger logger = LoggerFactory.getLogger(TaskfailoverStrategyType.class);
	private final  static int DEFAULT_MAX_INTERVAL_TIME = 1000*60;//最大间隔时间
	private final static int  DEFAULT_MAX_RETRY_NUMS = 10;//最大重试次数
	
	private final static int DEFAULT_INTERVAL_TIME_UNIT =  1000*6;//默认为一分钟
	
	private int maxIntervalTime = DEFAULT_MAX_INTERVAL_TIME ;//
	
	private int maxRetryNums = DEFAULT_MAX_RETRY_NUMS;
	
	private int intervalTimeUnit = DEFAULT_INTERVAL_TIME_UNIT;
	
	private int baseTime = 1;
	
	@Autowired
	private QuartzTaskManager quartzTaskManager;
	
	@Autowired
	private TaskJobFailMapper taskJobFailMapper;
	
	public TaskFailRetryStrategy(Integer maxRetryNums){
		if(maxRetryNums!=null) {
			this.maxRetryNums = maxRetryNums;
		}	 
	}
	
	@Override
	public void doHandler(TaskFailMessage taskFailMessage) {	  
		  int executeNum = taskFailMessage.getNum();
		  if(executeNum>=maxRetryNums) {//表示已经达到最大次数不能再重试
			  LogUtil.info(logger, "已经达到最大次数不再重试");
		  }else {
			  int  time = baseTime<<executeNum;
			  int  intervalTime = time *intervalTimeUnit;
			  if(intervalTime>maxIntervalTime) intervalTime  = maxIntervalTime;  
			  JobProvider jobProvider = new JobProvider();
			  jobProvider.setJobGroup(taskFailMessage.getJobGroup());
			  jobProvider.setJobName(taskFailMessage.getJobName());
			  jobProvider.setParams(taskFailMessage.getJobParams());
			  Calendar calendar = Calendar.getInstance();
			  calendar.add(Calendar.MILLISECOND, intervalTime);
			  quartzTaskManager.addJob(jobProvider, calendar.getTime()); 		  
		  }
		 //保存错误信息
		  saveTaskFailMessageToDatabase(taskFailMessage);
	}	
	
	/**
	 * 保存错误信息到数据库
	 * @param @param taskFailMessage
	 * @author guohao
	 * @date 2017年1月4日 下午6:32:34
	 * @since $
	 */
	private void saveTaskFailMessageToDatabase(TaskFailMessage taskFailMessage) {
		TaskJobFail taskJobFail = new TaskJobFail();
		taskJobFail.setFailTime(taskFailMessage.getFailTime());
		taskJobFail.setJobCode(taskFailMessage.getCode());
		taskJobFail.setJobName(taskFailMessage.getJobName());
		taskJobFail.setJobGroup(taskFailMessage.getJobGroup());
		taskJobFail.setJobParams(JSON.toJSONString(taskFailMessage.getJobParams()));
		taskJobFail.setNum(taskFailMessage.getNum());
		taskJobFailMapper.save(taskJobFail);	
	}
	public static void main(String[] args) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTimeInMillis(1483523652727l);
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");	
		System.out.println(sf.format(calendar.getTime()));
	}

}
